package com.plum.netty.demo;

import io.netty.channel.EventLoop;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.util.concurrent.DefaultPromise;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutionException;

@Slf4j
public class TestNettyPromise {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // 1. 准备 EventLoop 对象
        EventLoop eventLoop = new NioEventLoopGroup().next();
        // 2. 可以主动创建 promise 容器
        DefaultPromise<Object> promise = new DefaultPromise<>(eventLoop);
         new Thread(() -> {
             // 3. 任意一个线程执行，执行完毕后向 promise 添加数据
             log.debug("开始计算");
             try {
                 Thread.sleep(1000);
             } catch (InterruptedException e) {
                 e.printStackTrace();
                 promise.setFailure(e);
             }
             promise.setSuccess(88);
         }).start();

        log.debug("等待结果");
        log.debug("结果 = {}", promise.get());
    }
}
